﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace TextMIS
{
    public partial class frmTotalCost : Form
    {
        public frmTotalCost()
        {
            InitializeComponent();
        }

        public static string userName;
        public static string strSql;
        SqlConnection conn;
        SqlCommand com;
        SqlDataAdapter da;
        SqlDataReader dr;


        private void frmTotalCost_Load(object sender, EventArgs e)
        {
            conn = new
SqlConnection(frmLogin.SQLstr);
            listView1.GridLines = true;
            listView1.FullRowSelect = true;
            listView1.View = View.Details;
            listView1.Scrollable = true;
            listView1.MultiSelect = false;
            listView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;

            listView2.GridLines = true;
            listView2.FullRowSelect = true;
            listView2.View = View.Details;
            listView2.Scrollable = true;
            listView2.MultiSelect = false;
            listView2.HeaderStyle = ColumnHeaderStyle.Nonclickable;

            this.ShowItem1();
            this.ShowItem2();
            this.PredictSum1();
            this.PredictSum2();
            this.PredictSum3();
        }

        private void HeaderList1()
        {
            listView1.Columns.Add("注册编号", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("用户姓名", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("体检项目", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("项目价格", 120, HorizontalAlignment.Left);

        }
        private void HeaderList2()
        {
            listView2.Columns.Add("注册编号", 100, HorizontalAlignment.Left);
            listView2.Columns.Add("用户姓名", 100, HorizontalAlignment.Left);
            listView2.Columns.Add("体检项目", 100, HorizontalAlignment.Left);
            listView2.Columns.Add("项目价格", 120, HorizontalAlignment.Left);

        }

        private void ShowItem1()
        {
            try
            {
                listView1.Clear();
                da = new SqlDataAdapter("select RegisterID, Pname,ItemName, ItemPrice from viewPI_tab where PrePaid = '1'", conn);

                DataTable dt = new DataTable();
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                int count = da.Fill(dt);
                conn.Close();

                HeaderList1();
                foreach (DataRow dr in dt.Rows)
                {
                    ListViewItem lvi = new ListViewItem();
                    lvi.SubItems.Clear();
                    lvi.SubItems[0].Text = dr[0].ToString();
                    lvi.SubItems.Add(dr[1].ToString());
                    lvi.SubItems.Add(dr[2].ToString());
                    lvi.SubItems.Add(dr[3].ToString());
                    listView1.Items.Add(lvi);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ShowItem2()
        {
            try
            {
                listView2.Clear();
                da = new SqlDataAdapter("select RegisterID, Pname,ItemName, ItemPrice from viewPI_tab where PrePaid = '0'", conn);

                DataTable dt = new DataTable();
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                int count = da.Fill(dt);
                conn.Close();

                HeaderList2();
                foreach (DataRow dr in dt.Rows)
                {
                    ListViewItem lvi = new ListViewItem();
                    lvi.SubItems.Clear();
                    lvi.SubItems[0].Text = dr[0].ToString();
                    lvi.SubItems.Add(dr[1].ToString());
                    lvi.SubItems.Add(dr[2].ToString());
                    lvi.SubItems.Add(dr[3].ToString());
                    listView2.Items.Add(lvi);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void PredictSum1()
        {
            int sum = 0;
            strSql = "select sum(ItemPrice) from viewPI_tab where PrePaid = '1'";
            com = new SqlCommand(strSql, conn);
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            dr = com.ExecuteReader();
            if (dr.Read())
            {
                sum = Convert.ToInt32(dr[0].ToString());
            }
            conn.Close();
            lblPrePaid.Text = "已收费 " + sum.ToString() + " 元";
        }

        private void PredictSum2()
        {
            int sum = 0;
            strSql = "select sum(ItemPrice) from viewPI_tab where PrePaid = '0'";
            com = new SqlCommand(strSql, conn);
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            dr = com.ExecuteReader();
            if (dr.Read())
            {
                sum = Convert.ToInt32(dr[0].ToString());
            }
            conn.Close();
            lblNoPaid.Text = "待收费 " + sum.ToString() + " 元";
        }
        private void PredictSum3()
        {
            int sum = 0;
            strSql = "select sum(ItemPrice) from viewPI_tab ";
            com = new SqlCommand(strSql, conn);
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            dr = com.ExecuteReader();
            if (dr.Read())
            {
                sum = Convert.ToInt32(dr[0].ToString());
            }
            conn.Close();
            lblSum.Text = "预计共收费 " + sum.ToString() + " 元";
        }
    }
}
